package com.carpedil.smvc.controller;

import com.carpedil.smvc.po.User;
import com.carpedil.smvc.service.UserService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/** @author Administrator */
@Controller
public class UserController {
  @Autowired private UserService userService;

  @RequestMapping("/register")
  public String register(
      User user, @RequestParam("file") MultipartFile file, HttpServletRequest request) {
    // 获取文件名
    String filename = file.getOriginalFilename();
    assert filename != null;
    // 截取文件后缀
    String suffix = filename.substring(filename.lastIndexOf("."));
    // 生成防止文件名重复的uid
    String uid = UUID.randomUUID().toString();
    // 获取文件上传存放在服务器的物理地址
    String fileDir = request.getServletContext().getRealPath("files");
    // 生成新的存放路径和新的文件名(uid + suffix;)
    String newFilePathName = fileDir + "/" + uid + suffix;
    // 保存到sql数据库中的url地址
    String sqlPath = "files/" + uid + suffix;
    user.getUserDetail().setUserImg(sqlPath);

    // 业务操作
    boolean b = userService.insertUser(user);
    if (b) {
      try {
        // 将文件保存到服务器
        FileUtils.copyInputStreamToFile(file.getInputStream(), new File(newFilePathName));
      } catch (IOException e) {
        e.printStackTrace();
      }
    } else {
      return "error";
    }
    return "redirect:/index.jsp";
  }

  @RequestMapping("/login")
  public String login(User user, HttpServletRequest request, HttpSession session) {
    User u = userService.login(user);
    System.out.println("u = " + u);
    if (u != null) {
      List<User> users = userService.getAllUser();
      if (!users.isEmpty()) {
        request.setAttribute("users", users);
        session.setAttribute("loginUser", u);
        return "users";
      }
    }
    return "error";
  }

  @RequestMapping("/login2")
  public String login2(User user, HttpServletRequest request, HttpSession session) {
    User u = userService.login2(user);
    if (u != null) {
      List<User> users = userService.getAllUser();
      if (!users.isEmpty()) {
        request.setAttribute("users", users);
        session.setAttribute("loginUser", u);
        return "users";
      }
    }
    return "error";
  }

  @RequestMapping("/logout")
  public String logOut(HttpSession session) {
    session.removeAttribute("loginUser");
    return "users";
  }

  @RequestMapping("/jump")
  public String jump() {
    return "redirect:/index.jsp";
  }

  @RequestMapping("/query")
  public String queryById(@RequestParam("roleName") String roleName, HttpServletRequest request) {
    List<User> list = userService.findUserByRoleName(roleName);
    request.setAttribute("users", list);
    return "users";
  }
}
